package com.hospital.hms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;

import com.hospital.hms.exception.HMSErrorCode;
import com.hospital.hms.service.DoctorWorkPlanSchduleService;
import com.hospital.hms.utils.PageUtils;
import com.hospital.hms.common.CommonResult;
import com.hospital.hms.controller.form.*;
import com.hospital.hms.service.DoctorService;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/docter")
@Tag(name = "DoctorController",description = "医生管理模块接口")
@Slf4j
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private DoctorWorkPlanSchduleService doctorWorkPlanSchduleService;
    @Operation(summary = "分页显示医生信息接口")
    @PostMapping("/selectConditionByPage")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:UPDATE"}, mode = SaMode.OR)
    public CommonResult selectConditionByPage(@Valid @RequestBody SelectDoctorByPageForm form ){
        Map<String, Object> map = BeanUtil.beanToMap(form);

        int page = form.getPage();
        int length = form.getLength();
        int start =(page-1)*length;
        map.put("start",start);
        PageUtils pageUtils = doctorService.selectConditionByPage(map);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,pageUtils);
    }
    @Operation(summary = "根据Id查询医生详细信息接口")
    @PostMapping("/selectDoctorDetailById")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:UPDATE"}, mode = SaMode.OR)
    public CommonResult selectDoctorDetailById(@Valid @RequestBody SelectDoctorDetailByIdForm form){
        HashMap<String, Object> map = doctorService.selectDoctorDetailById(form.getId());
        return CommonResult.ok().put("doctor",map);
    }
    @Operation(summary = "插入医生")
    @PostMapping("/insert")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:UPDATE"}, mode = SaMode.OR)
    public CommonResult insert(@RequestBody @Valid InsertDoctorForm form){
        Map<String, Object> map = BeanUtil.beanToMap(form);
        //添加UUID
        map.put("uuid",IdUtil.simpleUUID().toUpperCase());
        //更改Tag的String[]数组为JSON类型的字符串
        map.replace("tag", JSONUtil.parseArray(form.getTag()).toString());
        doctorService.insert(map);
        return CommonResult.ok();
    }
    @PostMapping("/updatePhoto")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:UPDATE"}, mode = SaMode.OR)
    @Operation(summary = "照片上传")
    public CommonResult updatePhoto(@Param("file") MultipartFile file, @Param("doctorId") Integer doctorId) {
        doctorService.updatePicture(file,doctorId);
        return CommonResult.ok();
    }
    @PostMapping("/selectById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    @Operation(summary = "通过ID查询医生信息")
    public CommonResult searchById(@RequestBody @Valid SelectDoctorByIdForm form) {
        HashMap map = doctorService.selectById(form.getId());
        return CommonResult.ok(map);
    }
    @Operation(summary = "根据诊室ID查询医生信息")
    @PostMapping("/selectDoctorsBySubId")
    @SaCheckLogin
    public CommonResult selectDoctorsBySubId(@RequestBody @Valid SelectDoctorsBySubIdForm form) {
        try {
            ArrayList<HashMap> result = doctorService.selectDoctorBySubId(form.getDeptSubId());
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
        } catch (Exception e) {
            // 捕获异常并记录中文日志
            log.error("查询诊室ID为 {} 的医生信息时发生错误", form.getDeptSubId(), e);
            return CommonResult.error();
        }
    }
    @Operation(summary = "通过诊室ID和日期查询医生出诊信息")
    @PostMapping("/selectDoctorScheduleByDeptSubIdAndDate")
    @SaCheckPermission(value = {"ROOT", "SCHEDULE:SELECT"}, mode = SaMode.OR)
    public CommonResult selectDoctorScheduleByDeptSubIdAndDate(@RequestBody @Valid SelectDoctorScheduleByDeptSubIdAndDateForm form) {
        try {
            // 将表单数据转换为 Map
            Map<String, Object> param = BeanUtil.beanToMap(form);
            // 查询医生出诊排班数据
            ArrayList<HashMap> scheduleData = doctorWorkPlanSchduleService.selectDoctorScheduleByDeptSubIdAndDate(param);
            // 返回成功结果
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, scheduleData);
        } catch (Exception e) {
            // 记录异常日志
            log.error("查询医生出诊排班时出错，form: {}", form, e);
            // 返回统一的失败信息
            return CommonResult.error(HMSErrorCode.PLAN_NOT_FOUND.getMessage());
        }
    }
}
